package gameplaying;

import java.util.Scanner;

//给定有数字组成的字符串，长度小于等于6
//每次可以将数字变为小于该数字的数字或者移除数字0以及右边的数字
public class DigitalDeletions1404 {
	
	public static void main(String[] args) {
		long time = System.currentTimeMillis();
		DigitalDeletions1404 object = new DigitalDeletions1404();
		long endTime = System.currentTimeMillis();
		System.out.println((endTime - time) + "  ms");
		object.getRes();
	}
	
	public void getRes(){
		long time = System.currentTimeMillis();
		initSG();
		long endTime = System.currentTimeMillis();
		System.out.println((endTime - time) + "ms");
		Scanner scanner = new Scanner(System.in);
		while(scanner.hasNext()){
			String input = scanner.next();
			if(input.charAt(0) == '0'){
				System.out.println("Yes");
				continue;
			}
			int value = Integer.parseInt(input);
			if(sg[value] == 1){
				System.out.println("Yes");
			}else{
				System.out.println("No");
			}
		}
		scanner.close();
	}
	
	//这个由于可以改变其他堆，所以不能求子游戏的SG值，然后异或
	//但是由于长度足够小，所以我们可以暴力求出所有的状态的值
	//有了P、N态，我们进行相应的判断即可

	//对于一位而言，0 是必胜态，1是必败态，所以2~9都是必胜态
	
	private static final int MAX = 1000000; 
	int[] sg = new int[MAX];
	
	//思路没有问题，但是超时了
	public void initSG(){
		sg[1] = 0;
		sg[0] = 1;
		for(int i = 2; i <= 9; i++){
			sg[i] = 1; 
		}
		for(int i = 10; i < MAX; i++){
			int[] numbers = new int[6];
			int temp = i;
			int index = 0;
			while(temp != 0){
				numbers[index++] = temp % 10;
				temp /= 10;
			}
			//注意index为0代表个位
			boolean isN = false;
			label:
			for (int j = 0; j < index; j++) {
				if(numbers[j] == 0){
					if(sg[getNewValue(numbers, j)] == 0){
						//只要找到一个必败态的，则当前就是必胜态
						isN = true;
						break label;
					}
					continue;
				}
				for (int k = 0; k < numbers[j]; k++) {
					if(sg[getNewValue(numbers, j, k)] == 0){
						//只要找到一个必败态的，则当前就是必胜态
						isN = true;
						break label;
					}
				}
			}
			if(isN){
				sg[i] = 1;
			}else{
				sg[i] = 0;
			}
		}
	}
	
	public int getNewValue(int[] numbers, int index, int newValue){
		int res = 0;
		int base = 1;
		for(int i = 0; i < numbers.length; i++){
			if(i == index){
				res += newValue * base;
			}else{
				res += numbers[i] * base;
			}
			base *= 10;
		}
		return res;
	}
	
	//去掉0之后的新值
	public int getNewValue(int[] numbers, int index){
		int res = 0;
		int base = 1;
		for(int i = index + 1; i < numbers.length; i++){
			res += numbers[i] * base;
			base *= 10;
		}
		return res;
	}
}
